package org.ielts.domain.base;

import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.ielts.domain.Atom;
import org.ielts.domain.Sex;
import org.ielts.domain.log.SchoolLog;
import org.ielts.domain.security.User;
import org.ielts.util.DateUtil;

import com.yifeng.dao.data.DataUtil;

import net.sourceforge.pinyin4j.PinyinHelper;

@Entity
public class Student extends Atom {

	@ManyToOne(fetch = FetchType.EAGER)
	private User consultor;
	@ManyToOne(fetch = FetchType.EAGER)
	private User tutor;

	private String shortName;

	private String no;
	private String stuId;

	private String job;

	private Date birthday;

	@Enumerated(EnumType.STRING)
	private Sex sex = Sex.NotSet;

	private String pinyin;

	@OneToMany(mappedBy = "student")
	private Set<SignCourse> courses = new HashSet<SignCourse>();

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "student")
	private Set<SchoolLog> schoolLogs = new HashSet<SchoolLog>();

	public boolean isSigned() {
		return courses != null && courses.size() > 0;
	}

	private ConsultStatus consultStatus = ConsultStatus.Free;

	private boolean consultStarred;

	String mobile;
	String phone;
	String address;
	String zip;
	String education;

	private String email;

	public int getAge() {
		if (birthday != null) {
			Date now = new Date();
			Calendar calendarNow = Calendar.getInstance();
			calendarNow.setTime(now);

			Calendar c = Calendar.getInstance();
			c.setTime(birthday);

			int year = calendarNow.get(Calendar.YEAR) - c.get(Calendar.YEAR);

			return year;
		}

		return 0;
	}

	public void setName(String name) {
		super.setName(name);

		String pinyin = getPinYinHeadChar(name);
		this.setPinyin(pinyin);
	}

	public static String getPinYinHeadChar(String str) {

		String convert = "";
		for (int j = 0; j < str.length(); j++) {
			char word = str.charAt(j);
			String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
			if (pinyinArray != null) {
				convert += pinyinArray[0].charAt(0);
			} else {
				convert += word;
			}
		}
		return convert;
	}

	public enum ConsultStatus {
		Free, Consult, Intention, Sign
	}

	public User getTutor() {
		return tutor;
	}

	public void setTutor(User tutor) {
		this.tutor = tutor;
	}

	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	public ConsultStatus getConsultStatus() {
		return consultStatus;
	}

	public void setConsultStatus(ConsultStatus consultStatus) {
		this.consultStatus = consultStatus;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getStuId() {
		return stuId;
	}

	public void setStuId(String stuId) {
		this.stuId = stuId;
	}

	public Set<SignCourse> getCourses() {
		return courses;
	}

	public void setCourses(Set<SignCourse> courses) {
		this.courses = courses;
	}

	public User getConsultor() {
		return consultor;
	}

	public void setConsultor(User consultor) {
		this.consultor = consultor;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public boolean isConsultStarred() {
		return consultStarred;
	}

	public void setConsultStarred(boolean consultStarred) {
		this.consultStarred = consultStarred;
	}

	public Sex getSex() {
		return sex;
	}

	public void setSex(Sex sex) {
		this.sex = sex;
	}

	public String getPinyin() {
		return pinyin;
	}

	public void setPinyin(String pinyin) {
		this.pinyin = pinyin;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	public String getEducation() {
		return education;
	}

	public void setEducation(String education) {
		this.education = education;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Set<SchoolLog> getSchoolLogs() {
		return schoolLogs;
	}

	public void setSchoolLogs(Set<SchoolLog> schoolLogs) {
		this.schoolLogs = schoolLogs;
	}
}
